from django.shortcuts import render
from rest_framework.viewsets import ModelViewSet
from rest_framework.views import APIView
from rest_framework.response import Response
from user.models import UserModel
# 密码加密
from werkzeug.security import generate_password_hash
from user.ser import UserSer


class Login(APIView):

    # 登录
    def post(self, request):

        username = request.data.get("username")
        password = request.data.get("password")
        uaccount = request.data.get("uaccount")

        try:
            users = UserModel.objects.filter(username=username, upwd=password, uaccount=uaccount)
        except Exception as e:
            print("失败的原因", e)
            return Response("登录失败")
        user = UserSer(users,  many=True)

        return Response({
            "code": 200,
            "msg": "登录成功",
            "user": user.data,
        })

    # 获取全部用户信息
    def get(self, request):

        user = UserModel.objects.all()

        users = UserSer(user, many=True)
        print("user", users.data)
        return Response({"code": 200, "msg": "获取成功", "user": users.data})


class UserView(ModelViewSet):
    queryset = UserModel.objects.all()
    serializer_class = UserSer

